package han.chensing.latos.maker.windows.dialog.level;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ArraySelection;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.OrderedSet;

import java.util.List;

import han.chensing.latos.common.level.content.objects.CommonLine;
import han.chensing.latos.common.level.content.objects.CommonNote;
import han.chensing.latos.common.level.sample.Sample;
import han.chensing.latos.common.level.sample.SampleSet;
import han.chensing.latos.maker.V;
import han.chensing.latos.maker.res.Res;
import han.chensing.latos.maker.windows.dialog.base.BaseDialog;
import han.chensing.latos.maker.windows.widget.CScrollList;
import han.chensing.latos.maker.windows.widget.CUIList;

public class BatchAddDialog extends BaseDialog {

    CScrollList<SampleSet> sampleSetList;
    CScrollList<Sample> samplesList;
    TextButton doneButton,cancelButton;

    List<SampleSet> sampleSets = V.project.getSampleData().getSampleSets();

    public BatchAddDialog(CScrollList<CommonLine> lineCList, CommonLine commonLine) {
        super("Batch add");
        Skin skin = Res.get().get("skinBasic", Skin.class);
        setSize(800,400);

        sampleSetList=new CScrollList<>(skin,SampleSet::toString);
        sampleSetList.setBounds(7,10,150,366);
        sampleSetList.setData(sampleSets);
        addActor(sampleSetList);

        samplesList=new CScrollList<>(skin,Sample::toString);
        samplesList.setBounds(160,10,300,366);
        samplesList.getCList().setMultiSelective(true);
        addActor(samplesList);

        doneButton=new TextButton("Done",skin);
        doneButton.setBounds(720,10,70,30);
        addActor(doneButton);

        cancelButton=new TextButton("Cancel",skin);
        cancelButton.setBounds(720,50,70,30);
        addActor(cancelButton);

        moveToCenter();

        sampleSetList.update();

        sampleSetList.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                int selectedIndex = sampleSetList.getCList().getSelectedIndex();
                if (selectedIndex==-1)
                    return;
                SampleSet sampleSet = sampleSets.get(selectedIndex);
                samplesList.setData(sampleSet.getSamples());
                samplesList.update();
            }
        });

        cancelButton.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                remove();
            }
        });
        doneButton.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                ArraySelection<CUIList.CUIListItem<Sample>> selection = samplesList.getCList().getSelection();
                OrderedSet<CUIList.CUIListItem<Sample>> items = selection.items();
                for (CUIList.CUIListItem<Sample> item : items) {
                    CommonNote commonNote =new CommonNote();
                    commonNote.setNoteType(CommonNote.NoteType.Tap);
                    commonNote.setDistanceToLineCenter(0);
                    commonNote.setBindLine(commonLine);
                    commonNote.setStartTime(item.getValue().getTime());
                    commonNote.setDistanceHeight(0);
                    commonLine.getNotes().add(commonNote);
                }
                lineCList.update();
                remove();
            }
        });
    }
}
